package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 就近原则
 */
@RestController //@ResponseBody + @Controller的组合注解
@RequestMapping("/dept") //暴露一个公共的路径
@Api(tags = "部门管理接口", description = "部门管理接口描述")
public class DepartmentController {

    /**
     * 注入对象
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有部门接口", notes = "分页查询部门列表")
    public PageList<Department> findPage(@RequestBody DepartmentQuery query) {

        //query ，高级查询的参数  万能键alt+enter
        return departmentService.findPage(query);
    }

    /**
     * 查询所有
     *
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有部门接口", notes = "部门列表")
    public AjaxResult findAll() {

        return AjaxResult.createSuccess(departmentService.findAll());
    }

    /**
     * 查询部门树
     *
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/treeDept")
    @ApiOperation(value = "查询上级部门接口", notes = "上级部门列表")
    public AjaxResult tree() {

        return AjaxResult.createSuccess(departmentService.treeDept());
    }

    /**
     * 查询所有
     *
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/findParentDepts")
    @ApiOperation(value = "查询上级部门接口", notes = "上级部门列表")
    public AjaxResult findParentDepts() {

        return AjaxResult.createSuccess(departmentService.findParentDepts());
    }

    /**
     * 新增或者修改，没有id就是新增
     *
     * @param department
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改部门", notes = "没有id就是新增")
    public AjaxResult saveOrUpdate(@RequestBody @Valid Department department) {

        try {
            departmentService.saveOrUpdate(department);
            return AjaxResult.createSuccess();
        } catch (RuntimeException e) {
            e.printStackTrace();

            return AjaxResult.createError("kjsdfjlksdfjdslkfjdkl");
        }
    }

    /**
     * 根据id删除一个对象
     *
     * @return localhost/dept/10
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门", notes = "根据id删除一个对象")
    public AjaxResult delete(@PathVariable("id") Long id) {

        departmentService.delete(id);

        return AjaxResult.createSuccess();

    }

    /**
     * 批量删除
     *
     * @return localhost/dept
     * {"ids":[1,2,3]}
     */
    @PatchMapping
    @ApiOperation(value = "批量删除部门", notes = "批量删除部门")
    public AjaxResult batchDelete(@RequestParam("ids") Long[] ids) {

        departmentService.batchDelete(ids);

        return AjaxResult.createSuccess();

    }

}
